package com.validateCodeYzm.zpf.service;

import com.validateCodeYzm.zpf.lib.HttpRequestUtil;
import com.validateCodeYzm.zpf.lib.HttpResponseCollect;
import org.jsoup.Jsoup;

import java.io.File;
import java.nio.file.Paths;
import java.util.Date;
import java.util.HashMap;
import java.util.Scanner;

public class LoginService {

    //下载验证码 返回信息集合
    public HttpResponseCollect downValidateCode(){

        String path = LoginService.class.getResource("").getPath();

        File downFile = Paths.get(path, "validateCode.gif").toFile();

        return HttpRequestUtil.getMultipleFile("https://202.100.182.103:8194/VerifyCode.ashx", new HashMap<>(), httpRequest -> {
        }, downFile).map(httpResponseCollect -> {
            System.out.println("请求成功");
            System.out.println("返回cookie -> " + httpResponseCollect.getCookie());
            System.out.println("返回file -> " + httpResponseCollect.getFile().getPath());
            return httpResponseCollect;
        }).toGetOrElseThrow(RuntimeException::new);
    }


    /**
     * 登录 返回密匙
     * @param account 账号
     * @param password 密码
     * @return
     */
    public LoginData login(String account,String password){

        HttpResponseCollect httpResponseCollect = downValidateCode();

        System.out.println("请手动识别验证码图片:"+httpResponseCollect.getFile().getPath());

        Scanner sc = new Scanner(System.in);

        System.out.println("请输入验证码：");

        String code = sc.next();

        String api = "https://202.100.182.103:8194/login.ashx?callbackfun=jsonpCallback&action=PTLogin&_AccountName="+account+"&_Password="+password+"&_CheckCode="+code+"&_="+new Date().getTime();
        System.out.println("登录api:"+api);

        //尝试登录
        HttpRequestUtil.postWithMultipleInfo(api,"GET",new HashMap<>(),httpRequest -> {
            httpRequest.header("Cookie",httpResponseCollect.getCookie());
        }).consumer(httpResponseCollect2 -> {

            String body = httpResponseCollect2.getBody();
            if(!body.contains("1000"))
                throw new RuntimeException("登录错误:"+body);

        });

        //进入主页
        String indexPageUrl = "https://202.100.182.103:8194/Platform.aspx";
        return HttpRequestUtil.postWithMultipleInfo(indexPageUrl,"GET",new HashMap<>(),httpRequest -> {

            httpRequest.header("Cookie",httpResponseCollect.getCookie());

        }).map(httpResponseCollect3 -> {

            System.out.println("进入主页:");
            String body = httpResponseCollect3.getBody();
            //System.out.println(body);
            String secretApi = Jsoup.parse(body).select("script").get(4).attr("src");

            LoginData loginData = new LoginData();

            loginData.setYzmCookie(httpResponseCollect.getCookie());
            loginData.setLoginSecret8196(secretApi);

            return loginData;

        }).ifError("获取密匙失败").toGetOrElseThrow(RuntimeException::new);

    }


    /**
     * 登录8196服务器 返回cookie
     * @return
     */
    public String  loin8196(LoginData loginData){
        return HttpRequestUtil.postWithMultipleInfo(loginData.getLoginSecret8196(),"GET",new HashMap<>(), httpRequest -> {
                    httpRequest.header("Cookie",loginData.getYzmCookie());
                })
                .map(HttpResponseCollect::getCookie)
                .print()
                .ifError("8196服务器获取cookie失败")
                .toGetOrElseThrow(RuntimeException::new);
    }



}
